/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 23:13:59
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import java.util.List;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.common.enums.CoursewareStates;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.edu.bean.EduCourseLearnHistoryDto;
import com.joysuch.wwyt.edu.bean.EduCoursewareAuditBean;
import com.joysuch.wwyt.edu.bean.EduCoursewareEditBean;
import com.joysuch.wwyt.edu.bean.EduCoursewareListBean;
import com.joysuch.wwyt.edu.bean.EduCoursewareListBean.SearchEduCourseware;
import com.joysuch.wwyt.edu.entity.EduCourseware;
import com.joysuch.wwyt.edu.service.EduCourseLearnHistoryService;
import com.joysuch.wwyt.edu.service.EduCoursewareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/edu/courseware")
@Api(tags = "课件")
public class EduCoursewareController {

    private static final Logger logger = LoggerFactory.getLogger(EduCoursewareController.class);

    @Autowired
    private EduCoursewareService eduCoursewareService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;


    @Autowired
    private EduCourseLearnHistoryService eduCourseLearnHistoryService;

    @RequiresPermissions("edu:courseware:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduCoursewareEditBean entity) {
        eduCoursewareService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:courseware:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduCoursewareEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduCoursewareService.update(entity);
        return ResultBean.success("");
    }

 //   @RequiresPermissions("edu:courseware")
    @RequestMapping("page")
    public Object page(Pageable page, SearchEduCourseware condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCoursewareListBean> data = eduCoursewareService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

//    @RequiresPermissions("edu:courseware:audit")
    @RequestMapping("audit-page")
    public Object auditPage(Pageable page, SearchEduCourseware condition) {
        page = PageDataUtils.addOrderByDesc(page, "updateTime");
        Page<EduCoursewareListBean> data = eduCoursewareService.findAuditByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

 //   @RequiresPermissions("edu:courseware")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduCourseware condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        condition.setState(CoursewareStates.AUDIT_SUCCESS.getId());
        Page<EduCoursewareListBean> data = eduCoursewareService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(EduCourseware.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("edu:courseware:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        eduCoursewareService.delete(id);
        return ResultBean.success("");
    }

//    @RequiresPermissions("edu:courseware")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduCoursewareService.getById(id));
    }

    @RequiresPermissions("edu:courseware:update")
    @RequestMapping("apply-audit")
    public Object applyForAudit(@Valid @NotNull(field = "id") Long id) {
        eduCoursewareService.applyForAudit(id);
        return ResultBean.success(null);
    }

    @RequiresPermissions("edu:courseware:audit")
    @PostMapping("audit")
    @ApiOperation("课件审核")
    public Object audit(@RequestBody EduCoursewareAuditBean bean ) {
        if ("success".equals(bean.getState())) {
            eduCoursewareService.audit(bean.getId(), CoursewareStates.AUDIT_SUCCESS, bean.getRemake());
        } else if ("fail".equals(bean.getState())) {
            eduCoursewareService.audit(bean.getId(), CoursewareStates.AUDIT_FAILED, bean.getRemake());
        } else {
            return ResultBean.fail(304, "未知的状态:" + bean.getState());
        }
        return ResultBean.success(null);
    }

    @ApiOperation(value = "查询课件学习进度",response = EduCourseLearnHistoryDto.class)
    @RequestMapping(value = "progress",method = RequestMethod.GET)
    public Object progress(@Valid @NotNull(field = "id") Long id,@RequestParam(required = false) Long trainingId) {
        return ResultBean.success(eduCourseLearnHistoryService.getProgress(id, Context.getCurrentUserId(),trainingId));
    }
}
